package com.yyb.controller;

import com.yyb.annotation.AppPermission;
import com.yyb.annotation.Log;
import com.yyb.common.Result;
import com.yyb.domain.SysDept;
import com.yyb.dto.SysDeptAddDTO;
import com.yyb.dto.SysDeptUpdateDTO;
import com.yyb.enums.BusinessType;
import com.yyb.service.SysDeptService;
import com.yyb.vo.SysDeptVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述: [部门管理相关的API]
 * 
 * 
 */
@RestController
@RequestMapping("sysDept")
@Tag(name = "部门管理相关的API")
public class SysDeptController extends BaseController{

    @Resource
    private SysDeptService sysDeptService;


    /**
     * 查询所有部门数据返回树型结构的JSON
     *   GET http://127.0.0.1:8080/sysDept/listDept
     *   接口ID：148742201
     *   接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742201
     */
    @GetMapping("listDept")
    @AppPermission(value = "system:dept:query")
    @Operation(summary = "查询所有部门数据返回树型结构的JSON")
    public Result<List<SysDeptVO>> listDept(){
        return new Result<>(this.sysDeptService.listDept());
    }


    /**
     * 根据ID查询部门
     *   GET http://127.0.0.1:8080/sysDept/get/{deptId}
     *   接口ID：148742205
     *   接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742205
     */
    @GetMapping("get/{deptId}")
    @Operation(summary = "根据ID查询部门")
    @AppPermission(value = "system:dept:query")
    public Result<SysDept> get(@PathVariable Long deptId){
        return new Result<SysDept>(this.sysDeptService.selectByPrimaryKey(deptId));
    }


    /**
     * 添加部门
     *   POST http://127.0.0.1:8080/sysDept/save
     *   接口ID：148742202
     *   接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742202
     */
    @PostMapping("save")
    @Operation(summary = "添加部门")
    @AppPermission(value = "system:dept:add")
    @Log(title = "部门管理-添加",businessType = BusinessType.INSERT)
    public Result<?> save(@RequestBody @Validated SysDeptAddDTO sysDeptAddDTO){
        int row=this.sysDeptService.insert(sysDeptAddDTO);
        return this.toAjax(row>0);
    }


    /**
     * 修改部门
     *   PUT http://127.0.0.1:8080/sysDept/update
     *   接口ID：148742203
     *   接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742203
     */
    @PutMapping("update")
    @Operation(summary = "修改部门")
    @AppPermission(value = "system:dept:edit")
    @Log(title = "部门管理-修改",businessType = BusinessType.UPDATE)
    public Result<?> update(@RequestBody @Validated SysDeptUpdateDTO sysDeptUpdateDTO){
        int row=this.sysDeptService.updateByPrimaryKey(sysDeptUpdateDTO);
        return this.toAjax(row>0);
    }


    /**
     * 删除部门
     *   DELETE http://127.0.0.1:8080/sysDept/delete/{deptId}
     *   接口ID：148742204
     *   接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742204
     */
    @DeleteMapping("delete/{deptId}")
    @Operation(summary = "删除部门")
    @AppPermission(value = "system:dept:remove")
    @Log(title = "部门管理-删除",businessType = BusinessType.DELETE)
    public Result<?> delete(@PathVariable Long deptId){
        int row=this.sysDeptService.deleteByPrimaryKey(deptId);
        return this.toAjax(row>0);
    }
}
